package com.scau402.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.scau402.common.annotation.DataScope;
import com.scau402.common.core.domain.entity.SysUser;
import com.scau402.common.utils.DateUtils;
import com.scau402.common.utils.SecurityUtils;
import com.scau402.common.utils.StringUtils;
import com.scau402.common.utils.code.OutboundOrderCodeUtils;
import com.scau402.system.domain.SysTenant;
import com.scau402.system.service.ISysTenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.scau402.system.mapper.WmsOutboundOrderMapper;
import com.scau402.system.domain.WmsOutboundOrder;
import com.scau402.system.service.IWmsOutboundOrderService;

/**
 * 商品出库单Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-06-26
 */
@Service
public class WmsOutboundOrderServiceImpl implements IWmsOutboundOrderService 
{
    @Autowired
    private WmsOutboundOrderMapper wmsOutboundOrderMapper;

    @Autowired
    private ISysTenantService tenantService;

    /**
     * 查询商品出库单
     * 
     * @param id 商品出库单主键
     * @return 商品出库单
     */
    @Override
    public WmsOutboundOrder selectWmsOutboundOrderById(Long id)
    {
        return wmsOutboundOrderMapper.selectWmsOutboundOrderById(id);
    }

    /**
     * 查询商品出库单列表
     * 
     * @param wmsOutboundOrder 商品出库单
     * @return 商品出库单
     */
    @Override
    @DataScope(tenantAlias = "t", userAlias = "u")
    public List<WmsOutboundOrder> selectWmsOutboundOrderList(WmsOutboundOrder wmsOutboundOrder)
    {
        wmsOutboundOrder.setTenantId(SecurityUtils.getLoginUser().getTenantId());
        return wmsOutboundOrderMapper.selectWmsOutboundOrderList(wmsOutboundOrder);
    }

    /**
     * 新增商品出库单
     * 
     * @param wmsOutboundOrder 商品出库单
     * @return 结果
     */
    @Override
    public int insertWmsOutboundOrder(WmsOutboundOrder wmsOutboundOrder)
    {
        wmsOutboundOrder.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        wmsOutboundOrder.setCreateTime(DateUtils.getNowDate());
        wmsOutboundOrder.setTenantId(SecurityUtils.getLoginUser().getTenantId());
        return wmsOutboundOrderMapper.insertWmsOutboundOrder(wmsOutboundOrder);
    }

    /**
     * 修改商品出库单
     * 
     * @param wmsOutboundOrder 商品出库单
     * @return 结果
     */
    @Override
    public int updateWmsOutboundOrder(WmsOutboundOrder wmsOutboundOrder)
    {
        wmsOutboundOrder.setTenantId(SecurityUtils.getLoginUser().getTenantId());
        wmsOutboundOrder.setUpdateTime(DateUtils.getNowDate());
        return wmsOutboundOrderMapper.updateWmsOutboundOrder(wmsOutboundOrder);
    }

    /**
     * 批量删除商品出库单
     * 
     * @param ids 需要删除的商品出库单主键
     * @return 结果
     */
    @Override
    public int deleteWmsOutboundOrderByIds(Long[] ids)
    {
        return wmsOutboundOrderMapper.deleteWmsOutboundOrderByIds(ids);
    }

    /**
     * 删除商品出库单信息
     * 
     * @param id 商品出库单主键
     * @return 结果
     */
    @Override
    public int deleteWmsOutboundOrderById(Long id)
    {
        return wmsOutboundOrderMapper.deleteWmsOutboundOrderById(id);
    }

    @Override
    public String getMaxCode() {
        Long tenantId = SecurityUtils.getLoginUser().getTenantId();
        String userCode = SecurityUtils.getLoginUser().getUser().getUserCode();
        userCode = userCode.substring(1);
        Map<String, Object> params = new HashMap<>();
        // 设置参数值
        params.put("tenantId", tenantId);
        params.put("userCode", userCode);
        return wmsOutboundOrderMapper.getMaxCode(params);
    }

    @Override
    public String getOutboundOrderCode(String loginUserTenantCode, String userCode) throws InterruptedException {
        OutboundOrderCodeUtils outboundOrderCodeUtils = new OutboundOrderCodeUtils();
        return outboundOrderCodeUtils.getNewCode(loginUserTenantCode, userCode);
    }

    @Override
    public WmsOutboundOrder selectWmsInboundOrderByOrderCode(String orderCode) {
        return wmsOutboundOrderMapper.selectWmsInboundOrderByOrderCode(orderCode);
    }

    @Override
    public String getNewCode(SysUser user) throws InterruptedException {
        // 获取数据库中最大的编码
        String maxCode = getMaxCode();
        String userCode = user.getUserCode();

        // 返回maxCode为NULL，及当前数据库没有数据，从0开始
        if(StringUtils.isNull(maxCode)) {
            maxCode = "0";
        }

        // 获取当前用户所属租户编码
        Long loginUserTenantId = SecurityUtils.getLoginUser().getTenantId();
        String loginUserTenantCode = tenantService.selectSysTenantByTenantId(loginUserTenantId).getTenantCode();

        // 设置redis的当前编码
        OutboundOrderCodeUtils outboundOrderCodeUtils = new OutboundOrderCodeUtils();
        outboundOrderCodeUtils.setCurrentCodeToMax(maxCode, loginUserTenantCode, userCode);

        return getOutboundOrderCode(loginUserTenantCode, userCode);
    }
}
